package main

import "fmt"

func ForLoop() {
	for i := 0; i < 10; i++ {
		fmt.Println(i)
	}

	for i := 0; i < 10; {
		println(i)
		i++
	}
}

// 类似  while
func Loop2() {
	i := 0
	for i < 10 {
		i++
		fmt.Print(i)
	}
}

func ForArr() {
	arr := [3]int{1, 2, 3}
	for idx, val := range arr {
		fmt.Printf("idx = %d, val = %d\n", idx, val)
	}
}

func ForSlice() {
	slice := []int{1, 2, 3}
	for idx, val := range slice {
		fmt.Printf("idx = %d, val = %d\n", idx, val)
	}
}

func ForMap() {
	m := map[string]int{
		"key1": 100,
		"key2": 200,
		"key3": 300,
		"key4": 400,
		"key5": 500,
	}

	for k, v := range m {
		fmt.Printf("k = %s, v = %v\n", k, v)
	}

	for k := range m {
		fmt.Printf("key = %s,value = %d\n", k, m[k])
	}
}

// 不要对迭代参数取地址
func LoopBug() {
	users := []User{
		{
			name: "alen",
		},
		{
			name: "alex",
		},
		{
			name: "WuSir",
		},
	}

	m := make(map[string]*User)
	for _, v := range users {
		fmt.Printf("%p \n", &v)
		m[v.name] = &v
	}

	fmt.Printf("%v", m)
}

type User struct {
	name string
}

func LoopBreak() {
	i := 0
	for true {
		if i > 10 {
			break
		}
		i++
	}
}

func LoopContinue() {
	i := 0
	for i < 10 {
		i += 1
		if i%2 == 1 {
			continue
		}
		println(i)
	}
}
